﻿using System;
using System.Data;
using System.Data.Common;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using MySql.Data.MySqlClient;

namespace HYFamilyLibDataManager
{
	public class MySqlFamilyImgTable : IFamilyImgTable
	{
		public MySqlFamilyImgTable(FamilyLibDatabase db)
		{
			this.mDatabase = db;
		}

		public bool AddLargeImg(DbFamily family, Image img, DbTransaction tr)
		{
			byte[] value = null;
			if (img != null)
			{
				MemoryStream memoryStream = new MemoryStream();
				img.Save(memoryStream, ImageFormat.Jpeg);
				value = memoryStream.ToArray();
			}
			using (MySqlCommand mySqlCommand = ((MySqlConnection)this.mDatabase.Connection).CreateCommand())
			{
				mySqlCommand.Transaction = (MySqlTransaction)tr;
				mySqlCommand.CommandText = string.Format("insert into FamilyImgTable (FamilyGUID,LargeImg) values('{0}',{1})", family.Guid.ToString(), "?img");
				MySqlParameter mySqlParameter = new MySqlParameter("?img", MySqlDbType.Blob);
				mySqlParameter.Value = value;
				mySqlCommand.Parameters.Add(mySqlParameter);
				mySqlCommand.ExecuteNonQuery();
			}
			return true;
		}

		public bool Add2DImg(DbFamily family, Image img, DbTransaction tr)
		{
			byte[] value = null;
			if (img != null)
			{
				MemoryStream memoryStream = new MemoryStream();
				img.Save(memoryStream, ImageFormat.Jpeg);
				value = memoryStream.ToArray();
			}
			using (MySqlCommand mySqlCommand = ((MySqlConnection)this.mDatabase.Connection).CreateCommand())
			{
				mySqlCommand.Transaction = (MySqlTransaction)tr;
				mySqlCommand.CommandText = string.Format("insert into FamilyImgTable (FamilyGUID,TwoDImg) values('{0}',{1})", family.Guid.ToString(), "?img");
				MySqlParameter mySqlParameter = new MySqlParameter("?img", MySqlDbType.Blob);
				mySqlParameter.Value = value;
				mySqlCommand.Parameters.Add(mySqlParameter);
				mySqlCommand.ExecuteNonQuery();
			}
			return true;
		}

		public bool Add(DbFamily family, Image largeImg, Image twoDImg, DbTransaction tr)
		{
			byte[] value = null;
			if (largeImg != null)
			{
				MemoryStream memoryStream = new MemoryStream();
				largeImg.Save(memoryStream, ImageFormat.Jpeg);
				value = memoryStream.ToArray();
			}
			byte[] value2 = null;
			if (twoDImg != null)
			{
				MemoryStream memoryStream2 = new MemoryStream();
				twoDImg.Save(memoryStream2, ImageFormat.Jpeg);
				value2 = memoryStream2.ToArray();
			}
			using (MySqlCommand mySqlCommand = ((MySqlConnection)this.mDatabase.Connection).CreateCommand())
			{
				mySqlCommand.Transaction = (MySqlTransaction)tr;
				mySqlCommand.CommandText = string.Format("insert into FamilyImgTable (FamilyGUID,LargeImg,TwoDImg) values('{0}',{1},{2})", family.Guid.ToString(), "?largeImg", "?twoDImg");
				MySqlParameter mySqlParameter = new MySqlParameter("?largeImg", MySqlDbType.Blob);
				mySqlParameter.Value = value;
				mySqlCommand.Parameters.Add(mySqlParameter);
				MySqlParameter mySqlParameter2 = new MySqlParameter("?twoDImg", MySqlDbType.Blob);
				mySqlParameter2.Value = value2;
				mySqlCommand.Parameters.Add(mySqlParameter2);
				mySqlCommand.ExecuteNonQuery();
			}
			return true;
		}

		public Image GetLargeImg(DbFamily Family)
		{
			if (this.mDatabase.Connection.State == ConnectionState.Closed)
			{
				this.mDatabase.Connection.Open();
			}
			Image result = null;
			DataTable dataTable = this.mDatabase.ExecQuery(string.Format("select LargeImg from FamilyImgTable where FamilyGUID='{0}'", Family.Guid.ToString()));
			if (this.mDatabase.Connection.State == ConnectionState.Open)
			{
				this.mDatabase.Connection.Close();
			}
			if (dataTable != null && dataTable.Rows.Count > 0)
			{
				try
				{
					result = new Bitmap(new MemoryStream((byte[])dataTable.Rows[0]["LargeImg"]));
				}
				catch
				{
					return null;
				}
				return result;
			}
			return result;
		}

		public Image Get2DImg(DbFamily Family)
		{
			if (this.mDatabase.Connection.State == ConnectionState.Closed)
			{
				this.mDatabase.Connection.Open();
			}
			Image result = null;
			DataTable dataTable = this.mDatabase.ExecQuery(string.Format("select TwoDImg from FamilyImgTable where FamilyGUID='{0}'", Family.Guid.ToString()));
			if (this.mDatabase.Connection.State == ConnectionState.Open)
			{
				this.mDatabase.Connection.Close();
			}
			if (dataTable != null && dataTable.Rows.Count > 0)
			{
				try
				{
					result = new Bitmap(new MemoryStream((byte[])dataTable.Rows[0]["TwoDImg"]));
				}
				catch
				{
					return null;
				}
				return result;
			}
			return result;
		}

		public bool UpdateLargeImg(DbFamily family, Image img)
		{
			if (this.mDatabase.Connection.State == ConnectionState.Closed)
			{
				this.mDatabase.Connection.Open();
			}
			bool result;
			using (MySqlCommand mySqlCommand = ((MySqlConnection)this.mDatabase.Connection).CreateCommand())
			{
				mySqlCommand.CommandText = string.Format("update FamilyImgTable set LargeImg={0} where FamilyGUID='{1}'", "?img", family.Guid.ToString());
				MySqlParameter mySqlParameter = new MySqlParameter("?img", MySqlDbType.Blob);
				byte[] value = null;
				if (family.Image != null)
				{
					MemoryStream memoryStream = new MemoryStream();
					img.Save(memoryStream, ImageFormat.Jpeg);
					value = memoryStream.ToArray();
				}
				mySqlParameter.Value = value;
				mySqlCommand.Parameters.Add(mySqlParameter);
				if (mySqlCommand.ExecuteNonQuery() != -1)
				{
					if (this.mDatabase.Connection.State == ConnectionState.Open)
					{
						this.mDatabase.Connection.Close();
					}
					result = true;
				}
				else
				{
					if (this.mDatabase.Connection.State == ConnectionState.Open)
					{
						this.mDatabase.Connection.Close();
					}
					result = false;
				}
			}
			return result;
		}

		public bool UpdateSmallImg(DbFamily family, Image img)
		{
			if (this.mDatabase.Connection.State == ConnectionState.Closed)
			{
				this.mDatabase.Connection.Open();
			}
			bool result;
			using (MySqlCommand mySqlCommand = ((MySqlConnection)this.mDatabase.Connection).CreateCommand())
			{
				mySqlCommand.CommandText = string.Format("update FamilyImgTable set SmallImg={0} where FamilyGUID='{1}'", "?img", family.Guid.ToString());
				MySqlParameter mySqlParameter = new MySqlParameter("?img", MySqlDbType.Blob);
				byte[] value = null;
				if (family.Image != null)
				{
					MemoryStream memoryStream = new MemoryStream();
					img.Save(memoryStream, ImageFormat.Jpeg);
					value = memoryStream.ToArray();
				}
				mySqlParameter.Value = value;
				mySqlCommand.Parameters.Add(mySqlParameter);
				if (mySqlCommand.ExecuteNonQuery() != -1)
				{
					if (this.mDatabase.Connection.State == ConnectionState.Open)
					{
						this.mDatabase.Connection.Close();
					}
					result = true;
				}
				else
				{
					if (this.mDatabase.Connection.State == ConnectionState.Open)
					{
						this.mDatabase.Connection.Close();
					}
					result = false;
				}
			}
			return result;
		}

		public bool Update2DImg(DbFamily family, Image img)
		{
			if (this.mDatabase.Connection.State == ConnectionState.Closed)
			{
				this.mDatabase.Connection.Open();
			}
			bool result;
			using (MySqlCommand mySqlCommand = ((MySqlConnection)this.mDatabase.Connection).CreateCommand())
			{
				mySqlCommand.CommandText = string.Format("update FamilyImgTable set TwoDImg={0} where FamilyGUID='{1}'", "?img", family.Guid.ToString());
				MySqlParameter mySqlParameter = new MySqlParameter("?img", MySqlDbType.Blob);
				byte[] value = null;
				if (family.Image != null)
				{
					MemoryStream memoryStream = new MemoryStream();
					img.Save(memoryStream, ImageFormat.Jpeg);
					value = memoryStream.ToArray();
				}
				mySqlParameter.Value = value;
				mySqlCommand.Parameters.Add(mySqlParameter);
				if (mySqlCommand.ExecuteNonQuery() != -1)
				{
					if (this.mDatabase.Connection.State == ConnectionState.Open)
					{
						this.mDatabase.Connection.Close();
					}
					result = true;
				}
				else
				{
					if (this.mDatabase.Connection.State == ConnectionState.Open)
					{
						this.mDatabase.Connection.Close();
					}
					result = false;
				}
			}
			return result;
		}

		public bool Remove(DbFamily family, DbTransaction tr)
		{
			bool result;
			try
			{
				this.mDatabase.ExecuteCommand("delete from FamilyImgTable where FamilyGUID='" + family.Guid.ToString() + "'", tr);
				result = true;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			return result;
		}

		private FamilyLibDatabase mDatabase;
	}
}
